package com.zx.article.articleType.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zx.article.articleType.model.ArticleTypeModel;
import com.zx.article.articleType.service.IArticleTypeService;
import com.zx.article.articleType.service.impl.ArticleTypeServiceImpl;
import com.zx.article.util.FmtRequest;

@WebServlet("/ArticleTypeServlet")
public class ArticleTypeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private FmtRequest fmtRequest = new FmtRequest();
	private IArticleTypeService articleTypeService = new ArticleTypeServiceImpl();

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Object result = "未实现方法";
		String action = request.getParameter("action");
		switch (action) {
		case "add":
			result = add(request);
			break;
		case "getAll":
			result = getAll(request);
			break;
		case "get":
			result = list(request);
			break;
		case "del":
			result = del(request);
			break;
		case "upd":
			result = upd(request);
			break;
		default:
			break;
		}
		fmtRequest.write(response.getWriter(), result);
	}

	private Object upd(HttpServletRequest request) {
		ArticleTypeModel articleTypeModel = new ArticleTypeModel(request.getParameter("name"));
		articleTypeModel.setId(Integer.parseInt(request.getParameter("id")));
		return articleTypeService.upd(articleTypeModel);
	}

	private String del(HttpServletRequest request) {
		String name = request.getParameter("name");
		return articleTypeService.del(name);
	}

	private Object list(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<>();
		// 1-count
		ArticleTypeModel articleTypeModel = new ArticleTypeModel(request.getParameter("name"));
		System.out.println(articleTypeModel.getName());
		int count = articleTypeService.getCount(articleTypeModel);
		map.put("count", count);
		// 2-list
		articleTypeModel.setPageIndex(Integer.parseInt(request.getParameter("pageIndex")));
		articleTypeModel.setPageLimit(Integer.parseInt(request.getParameter("pageLimit")));
		articleTypeModel.setPageOn(true);
		List<ArticleTypeModel> list = articleTypeService.getAll(articleTypeModel);
		map.put("list", list);
		return map;
	}

	private List<ArticleTypeModel> getAll(HttpServletRequest request) {
		return articleTypeService.getAll(new ArticleTypeModel());
	}

	private String add(HttpServletRequest request) {
		String name = request.getParameter("name");
		return articleTypeService.add(name);
	}

}
